/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Yohanes Beny P
 */
@WebServlet(urlPatterns = {"/FilterLogin"})
public class FilterLogin extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String id = request.getParameter("NAMA");
            String pass = request.getParameter("PASSWORD");
            Connection con = Database.Koneksi.open();
            Statement st = con.createStatement();
            String sql = "select * from LOGIN_T";
            ResultSet rs = st.executeQuery(sql);
            boolean status = false;
            while (rs.next()) {
                if ((rs.getString("NAMA") == null ? id == null : rs.getString("NAMA").equals(id)) && (rs.getString("PASSWORD") == null ? pass == null : rs.getString("PASSWORD").equals(pass))) {
                    status = true;
                    break;
                } else {
                    status = false;
                }
            }
            if (("Admin".equals(id)) && ("admin".equals(pass))) {
                RequestDispatcher reg = request.getRequestDispatcher("HomePusat.jsp");
                reg.forward(request, response);
            } else if (status == true) {
                Cookie ck = new Cookie("user", id);
                response.addCookie(ck);
                RequestDispatcher reg = request.getRequestDispatcher("HomeCabang.jsp");
                reg.forward(request, response);
            } else if (status == false) {
                RequestDispatcher reg = request.getRequestDispatcher("FormLogin.jsp");
                reg.forward(request, response);
            }
        } catch (SQLException ex) {
            Logger.getLogger(FilterLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
//        try {
//            response.setContentType("text/html;charset=UTF-8");
//            PrintWriter out = response.getWriter();
//            String id = request.getParameter("ID_KANTOR_CABANG");
//            String pass = request.getParameter("PASSWORD");
//            Connection con = Database.Koneksi.open();
//            Statement st = con.createStatement();
//            String sql = "select * from KANTOR_CABANG";
//            ResultSet rs = st.executeQuery(sql);
//            boolean status = false;
//            while (rs.next()) {
//                if ((rs.getString("ID_KANTOR_CABANG") == null ? id == null : rs.getString("ID_KANTOR_CABANG").equals(id)) && (rs.getString("PASSWORD") == null ? pass == null : rs.getString("PASSWORD").equals(pass))) {
//                    status = true;
//                    break;
//                } else {
//                    status = false;
//                }
//            }
//            if ((id.equals("admin")) && (pass.equals("admin"))) {
//                RequestDispatcher reg = request.getRequestDispatcher("KantorPusat/HomePusat.jsp");
//                reg.forward(request, response);
//            } else if (status == true) {
//                    RequestDispatcher reg = request.getRequestDispatcher("KantorCabang/HomeCabang.jsp");
//                    reg.forward(request, response);
//                } else if (status == false) {
//                    RequestDispatcher reg = request.getRequestDispatcher("FormLogin.jsp");
//                    reg.forward(request, response);
//                }
//        } catch (SQLException ex) {
//            Logger.getLogger(FilterLogin.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}